//
// Created by LiuYou on 2021/5/17.
//

#ifndef HIGHPERFORMANCELINUXSERVERPROGRAMMING_LINUX_USERINFO_HPP
#define HIGHPERFORMANCELINUXSERVERPROGRAMMING_LINUX_USERINFO_HPP


#include <string>
#include <ostream>
#include <vector>
#include <fstream>
#include <unistd.h>

namespace ClientEntry {
    class UserInfo {
    public:
        UserInfo();

        UserInfo(std::string name, std::string passwd);

        virtual ~UserInfo();

        const std::string& getName() const;

        void setName(const std::string& name);

        const std::string& getPasswd() const;

        void setPasswd(const std::string& passwd);

    public:
        friend std::ostream& operator<<(std::ostream& os, const UserInfo& info);

        friend bool operator==(const UserInfo& lhs, const UserInfo& rhs);

        friend bool operator!=(const UserInfo& lhs, const UserInfo& rhs);


    private:
        std::string name;
        std::string passwd;

    };


    class Users {
    public:

#ifdef reference

        void updateQuestions() {
            std::cout << std::endl << "题目查询(更新题目)，输入题目难度系数进行修改更新: ";
            int difficulty{0};
            std::cin >> difficulty;
            int index{0};
            for (int i = 0; i < length; ++i) {
                if (element[i].getDifficulty() == difficulty) {
                    std::cout << i << "> " << element[i] << std::endl;
                    ++index;
                }
            }
            std::cout << "题目难度: " << difficulty << ", 题目类型: " << element[0].getQuestionType() << ", 题目数量: " << index
                      << std::endl;
            int theNumberOfQuestion{0};
            std::cout << "想要更新的题目: ";
            std::cin >> theNumberOfQuestion;
            std::cout << "修改题目难度: ";
            std::cin >> difficulty;
            std::cin.get();
            element[theNumberOfQuestion].setDifficulty(difficulty);
            std::cout << "修改题目: ";
            std::string theContentOfQuestion;
            std::getline(std::cin, theContentOfQuestion);
            element[theNumberOfQuestion].setQuestion("题目: " + theContentOfQuestion);
        }



        /*
         * 存储问题的文件的信息。
         */
        void infoOfQuestionsFile() const {
            for (int i = 0; i < length; ++i) {
                std::cout << i << "> " << element[i] << std::endl;
            }
            std::cout << "题目类型: " << element[1].getQuestionType() << ", 题目数量: " << length << std::endl;
        }

        /*
         * 向指定文件写入题目进行存储。
         */
        void writeQuestionsToFile(std::string filePath, int questionType) {
            std::ofstream outputFileStream;
            const char* file[5]{"CalculationQuestions", "ApplicationQuestions", "CompletionQuestions",
                                "ChoiceQuestions", "BrainQuestions"};
            outputFileStream.open(filePath + file[questionType - 1]);
            for (int i = 0; i < length; ++i) {
                outputFileStream << element[i].getDifficulty() << std::endl << "---"
                                 << std::endl
                                 << element[i].getQuestionType() << std::endl << std::endl
                                 << element[i].getQuestion() << std::endl << "----"
                                 << std::endl
                                 << std::endl;
            }
            outputFileStream.close();
        }
        /*
     * 添加问题到文件末尾。
     */
        std::string addQuestionToFile(const char** file, int questionType) {
            std::ofstream outputFileStream;
            // 将新的题目添加到文件末尾
            outputFileStream.open(static_cast<std::string>("QuestionRepository/") + file[questionType - 1],
                                  std::ofstream::app);
            std::cout << std::endl << "下面增加题目，题目难度: ";
            int difficulty{0};
            std::cin >> difficulty;
            std::cout << "题目类型: ";
            std::string theTypeOfQuestion;
            std::cin >> theTypeOfQuestion;
            // 读取回车字符
            std::cin.get();
            std::cout << "题目: ";
            std::string question;
            std::getline(std::cin, question);

            outputFileStream << std::endl << difficulty << std::endl << "---" << std::endl
                             << theTypeOfQuestion << std::endl << std::endl
                             << "题目: " << question << std::endl << "----" << std::endl;
            outputFileStream.close();
            return file[questionType - 1];
        }

        /*
         * 删除题目指定的题目。
         */
        Users& remove(int theNumberOfQuestion) {
            Tp value{element[theNumberOfQuestion]};
            for (int i = theNumberOfQuestion; i < length; ++i) {
                if (i == length - 1) {
                    break;
                }
                element[i] = element[i + 1];
            }

            // 题目减一
            --length;
            return *this;
        }

        /*
         * 删除题目。
         */
        Users& remove() {
            std::cout << std::endl << "下面删除题目，输入题目难度系数进行删除: ";
            int difficulty{0};
            std::cin >> difficulty;
            int index{0};
            for (int i = 0; i < length; ++i) {
                if (element[i].getDifficulty() == difficulty) {
                    std::cout << i << "> " << element[i] << std::endl;
                    ++index;
                }
            }
            std::cout << "该难度系数共有 " << index << " 道试题" << std::endl;
            int theNumberOfQuestion{0};
            std::cout << "想要移除的题目: ";
            std::cin >> theNumberOfQuestion;

            Tp value{element[theNumberOfQuestion]};
            for (int i = theNumberOfQuestion; i < length; ++i) {
                if (i == length - 1) {
                    break;
                }
                element[i] = element[i + 1];
            }

            // 题目减一
            --length;
            return *this;
        }

#endif


        bool queryUsersByName(const std::string& name) const {
            for (const auto& item : users) {
                if (item.getName() == name) {
                    return true;
                }
            }
            return false;
        }

        /*
         * 从文件中读取所有的用户。
         */
        void readUsersInfoFromFile(/*const std::string& fileName*/) {
            // 从文件读取信息
            std::ifstream inputFileStream;
            char buffer[1024]{};
            getcwd(buffer, sizeof(buffer));
            std::string usersInfoFile(buffer);
            usersInfoFile.append("/UsersInfo");
            inputFileStream.open(usersInfoFile);
            std::string line;
            // 读取文件
            while (std::getline(inputFileStream, line)) {
                UserInfo userInfo;
                // 格式: 用户名:密码
                std::string passwd = line.substr(line.find(':') + 1);
                std::string name = line.substr(0, line.find(':'));
                userInfo.setName(name);
                userInfo.setPasswd(passwd);
                users.push_back(userInfo);
            }
            inputFileStream.close();
        }


    public:
        Users();

    private:
        std::vector<UserInfo> users;

    };

}


#endif //HIGHPERFORMANCELINUXSERVERPROGRAMMING_LINUX_USERINFO_HPP
